package conf

import (
	"time"
)

var config *Config

type SendMsg struct {
	Msgtype string            `json:"msgtype" toml:"msgtype" env:"Msgtype"`
	Text    map[string]string `json:"text" toml:"text" env:"Text"`
	At      At                `json:"at" toml:"at" env:"At"`
}

func GetDefaultSendMsg() *SendMsg {

	return &SendMsg{
		Msgtype: "",
		Text:    map[string]string{"content": "this  message"},
		At:      *NewAt(),
	}
}

type Config struct {
	Msgtype string            `json:"msgtype" toml:"msgtype" env:"Msgtype"`
	Text    map[string]string `json:"text" toml:"text" env:"Text"`
	At      At                `json:"at" toml:"at" env:"At"`
	URL     string            `json:"url" toml:"url" env:"URL"`
}

func GetConf() *Config {

	return config
}
func SetConf(url, user, mobile string) *Config {

	config.URL = url
	config.At.AtMobiles = append(config.At.AtMobiles, mobile)
	config.At.AtUserIds = append(config.At.AtUserIds, user)
	return config
}

type At struct {
	AtMobiles []string `json:"atMobiles" toml:"atMobiles" env:"AtMobiles"`
	AtUserIds []string `json:"atUserIds" toml:"atUserIds" env:"AtUserIds"`
	IsAtAll   bool     `json:"isAtAll" toml:"isAtAll" env:"IsAtAll"`
}

func NewAt() *At {
	return &At{
		AtMobiles: []string{"mobiles_number"},
		AtUserIds: []string{"user_name"},
		IsAtAll:   false,
	}
}

func NewDefaultConf() *Config {

	return &Config{
		Msgtype: "text|json",
		Text:    map[string]string{"content": "this  message"},
		At:      *NewAt(),
		URL:     "dingding webhook url ",
	}

}

type ResMsg struct {
	Receiver          string            `json:"receiver"`
	Status            string            `json:"status"`
	Alerts            []Alert           `json:"alerts"`
	GroupLabels       map[string]string `json:"groupLabels"`
	CommonLabel       map[string]string `json:"commonLabel"`
	CommonAnnotations map[string]string `json:"commonAnnotations"`
	ExternalURL       string            `json:"externalURL"`
	Version           string            `json:"version"`
	GroupKey          string            `json:"groupKey"`
	TruncatedAlerts   string            `json:"truncatedAlerts"`
}

type Alert struct {
	Labels      map[string]string `json:"labels"`
	Annotations map[string]string `json:"annotations"`
	StartsAt    time.Time         `json:"startsAt"`
	EndsAt      time.Time         `json:"endsAt"`
}

func NewAlert() *Alert {
	return &Alert{
		Labels:      map[string]string{},
		Annotations: map[string]string{},
		StartsAt:    time.Now(),
		EndsAt:      time.Now(),
	}

}

type DingResponse struct {
	Errcode int32  `json:"errcode"`
	Errmsg  string `json:"errmsg"`
}
